package com.weijianhuawen.wjblog.search.runner;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.weijianhuawen.wjblog.common.constants.Constants;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleContentDo;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleContentMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleMapper;
import com.weijianhuawen.wjblog.search.config.LuceneProperties;
import com.weijianhuawen.wjblog.search.helper.LuceneHelper;
import com.weijianhuawen.wjblog.search.index.ArticleIndex;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.apache.lucene.document.Document;
import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-04 17:37
 */

@Component
@Slf4j
public class InitLuceneIndexRunner implements CommandLineRunner {
    @Autowired
    private LuceneProperties luceneProperties;
    @Autowired
    private LuceneHelper luceneHelper;
    @Resource
    private ArticleContentMapper articleContentMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Override
    public void run(String... args) throws Exception {
        log.info("【初始化Lucene索引】 开始初始化Lucene索引");

        // 查询所有文章
        List<ArticleDo> articleDoList = articleMapper.selectList(Wrappers.emptyWrapper());
        // 未发布文章，则不创建lucene索引
        if (articleDoList.isEmpty()) {
            log.info("【初始化 LUCENE】 未发布任何文章，暂不创建索引");
            return;
        }
        // 若配置文件中，没有存放索引文件存放目录，则不创建索引
        if (StringUtils.isBlank(luceneProperties.getIndexDir())) {
            log.error("【初始化 LUCENE】 索引文件目录未配置，请在yml配置索引文件");
            return;
        }
        // 索引存放目录
        String articleIndex = ArticleIndex.NAME;
        List<Document> documents = Lists.newArrayList();
        articleDoList.forEach(articleDo -> {
            Long articleId = articleDo.getId();

            // 查询文章正文
            ArticleContentDo articleContentDo = articleContentMapper.selectByArticleId(articleId);
            // 构建文档
            Document document = new Document();
            // 设置文章字段
            document.add(new TextField(ArticleIndex.COLUMN_ID, String.valueOf(articleId), Field.Store.YES));
            document.add(new TextField(ArticleIndex.COLUMN_TITLE, articleDo.getTitle(), Field.Store.YES));
            document.add(new TextField(ArticleIndex.COLUMN_CONTENT, articleContentDo.getContent(), Field.Store.YES));
            document.add(new TextField(ArticleIndex.COLUMN_COVER, articleDo.getCover(), Field.Store.YES));
            document.add(new TextField(ArticleIndex.COLUMN_CREATE_TIME, Constants.DATE_TIME_FORMATTER.format(articleDo.getCreateTime()), Field.Store.YES));
            document.add(new TextField(ArticleIndex.COLUMN_SUMMARY, articleDo.getSummary(), Field.Store.YES));
            documents.add(document);
        });
        // 创建索引
        luceneHelper.createIndex(articleIndex, documents);
        log.info("【初始化 LUCENE】 初始化索引完成！");
    }
}
